<?php 
session_start();
require_once ("class/m_order.php");
require_once ("class/m_stock.php");
require_once ("class/m_sendSMS.php");
require_once ("include/function_SMS.php");

	$HeadOffice=$HO_ID;

	$m = new M_order();
	$m_stock=new M_stock();
	$user=$_SESSION['username'];
	$uid=$_SESSION['userid'];

if (isset($_POST['m'])  &&  $_POST['m']!=""){
	if ($_POST['m']== 'view'){
		if(isset($_POST['id']) && $_POST['id']!=""){ 
			$id=$_POST['id'];
			$type=$_POST['type'];
			$page=isset($page)?$page:1;
			
			$resultHeader =$m->ViewHeader($id);
			
			$result_detail=$m->ViewDetail($id);
 						
			$rows=$m->getRowCount();
			
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			 
			if ($rows>0){
				while($row = $result_detail->FetchRow()){
					If($i%2 <> 0){
						if ( $type=='cl'){
							if ($row['Amount_s'] <> $row['Amount_r'])
								$html .= "<tr class=row1 style='color:red'>";
							else 
								$html .= "<tr class=row1 >";
						}else if ($type=='vcl'){
							if ($row['Amount_r'] <> $row['Amount_c'])
								$html .= "<tr class=row1 style='color:red'>";
							else 
								$html .= "<tr class=row1 >";						
						}else{
							$html .= "<tr class=row1>";
							}
					} else{
						if ( $type=='cl'){
							if ($row['Amount_s'] <> $row['Amount_r'])
								$html .= "<tr style='color:red'>";
							else 
								$html .= "<tr >";
						}else if ($type=='vcl'){
							if ($row['Amount_r'] <> $row['Amount_c'])
								$html .= "<tr class=row1 style='color:red'>";
							else 
								$html .= "<tr class=row1 >";									
						}else{
							$html .= "<tr>";
							}
					}
					
					$html .= getTableDetail($row,$i,$type)."</tr>";
					 
					$i++;
				}
			}else{
					$html="<tr>"
					."<td colspan='8' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'header'=> json_encode($resultHeader),
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
							
			echo  json_encode( $ar_result );

		}
		
		//******************************
		//****   Confirm 
		//******************************
	}else if ($_POST['m']=='confirm'){
			$id=$_POST['o_id'];
			$list= $_POST['var'];//{'id': oid , 'amt':amt,'pid':pid }
			$branchid=$_SESSION['bid'];
			$order_no=$_POST['no'];//orderno
			$branchName=$_POST['branchname'];
			$HOID=$m_stock->GetHO_ID();
			
			foreach ($list as $val) {
				 $HO_amt = $val['h_amt'];
				 $ActualAmt =  $HO_amt - $val['amt']; // Amount from HO - Actural receive AMT 
				 $m->UpdateOrderProduct($val['id'],$val['amt'],'cl');
				
				 $m_stock->UpdateStock($val['pid'], $branchid , $val['amt'] ,$user);
				
				// if actual amount  > 0 or < 0 
				 if ($ActualAmt <> 0 ) {
				   $m_stock->MoveFromTempStock($val['pid'],$ActualAmt,$HOID);
				 }
			}

			$m->updateOrderStatus($id,$user, $uid ,4);		
			
			$listUser = $m->getUserAdmin_Tele_Email();	
			 
			if ($listUser['Count'] > 0 ){
				$msg=SMSMessage_ConfirmReceive($order_no , $branchName);
				//Send SMS
				//print $msg;
				$s=new SMS($listUser['Tele'],$msg);
				$s->SendSMS();
				$s=null;
				
				//Send Email
				$headers = "From:" . $Emailsender;
				
				if ($listUser['Email'] != ""){
					mail($listUser['Email'] ,"Confirm Received Product" ,$msg,$headers);
				}	
				// End send SMS
			}
			
			 echo "completed";	
	
		//******************************
		//****   checking and return to Branch
		//******************************
	}else if ($_POST['m']=='check'){
			$id=$_POST['o_id'];
			$list= $_POST['var'];
			$order_no=$_POST['no'];//orderno
			$branchid=$_SESSION['bid'];	//admin	
			$b="";
			foreach ($list as $val) {
				$m->UpdateOrderProduct($val['id'],$val['amt'],'c');
				$m->up_to_TempStock($branchid, $val['pid'], $val['amt']);
			}
			//print 
			//echo $b;	
			$m->updateOrderStatus($id,$user, $uid ,3);		

			//Function Send SMS

			$listUser = $m->getUserOrderBranch($id);	
			//Send SMS
			if ($listUser['Count'] > 0 ){
				$msg=SMSMessage_FromHO($order_no);
				$s=new SMS($listUser['Tele'],$msg);
				$s->SendSMS();
				$s=null;
				
				//Send Email
				$headers = "From:" . $Emailsender;
				if ($listUser['Email'] !=""){
					mail($listUser['Email'] ,"Check completed" ,$msg,$headers);
				}
				// End send SMS
			}
			echo "completed";	
	}
	//******************************
	//****       Send to HO
	//******************************
	else if ($_POST['m']== 'send'){
			$id=$_POST['id'];
			$order_no=$_POST['no'];
			$branchName=$_POST['branchname'];
			$ItemNumber=$_POST['itemnumber'];
			//Update 
			$m->updateOrderStatus($id,$user, $uid ,2);
		
			//Function Send SMS
			$listUser = $m->getUserAdmin_Tele_Email();	
			if ($listUser['Count'] > 0 ){
				$msg=SMSMessage_FromBranch($order_no , $branchName , $ItemNumber);
				//Send SMS
				$s=new SMS($listUser['Tele'],$msg);
				$s->SendSMS();
				$s=null;
				
				//Send Email
				$headers = "From:" . $Emailsender;
				if ($listUser['Email'] !=""){
					mail($listUser['Email'] ,"Send Order" ,$msg,$headers);
				}
				// End send SMS
			}
			echo "completed";
	}
}else{
			echo "error"	;
	}

	function getTableDetail($row,$i,$type){
		$html .="<td align='right'>".$i."</td>";
		$html .="<td style='display:none'>".$row['ID']."</td>";
		$html .="<td style='display:none'>".$row['ProductID']."</td>";
		$html .="<td align='left'>".$row['ProductName']."</td>";
		$html .="<td  align='left'>".$row['CatName']."</td>";
		$html .="<td  align='center'>".$row['ProductType']."</td>";
		$html .="<td  align='center'>".$row['Unit']."</td>";
		// $html .="<td  align='right'>".number_format(  $row['RemainAmt'] ,'0','.',',')."</td>";
		$html .="<td  align='right'>".number_format(  $row['RemainAmt'] ,'0','.',',')."&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;<font color='blue'>". number_format( $row['Amount_s'],'0','.',',')."</font></td>";
		//Check By Ho
		 if ($type=='c'){
			 $html .="<td  align='right'><span name='remainHo'>".number_format($row['RemainHOAmt'],'0','.',',')."</span>&nbsp;/&nbsp;<input type='text'  style='text-align:right;' class='amtBox' name='amt_c' id='amt_c' maxlength='4' size='5' value=".number_format( $row['Amount_s'],'0','.',',')."></td>";
			 $html .="<td  align='right'>". number_format( $row['RAmt'],'0','.',',')."</td>";
		 }else if ($type=='vc'){
			$html .="<td  align='right'>". number_format($row['RemainHOAmt'],'0','.',',')."&nbsp;&nbsp;/&nbsp;&nbsp;". number_format( $row['Amount_r'],'0','.',',')."</td>";
		}else if ($type=='cl' ){
			$html .="<td  align='right'><span name='HOSendBack'>". number_format( $row['Amount_r'],'0','.',',')."</span></td>";
			$html .="<td  align='right'><input type='text'  style='text-align:right;' class='amtBox' name='amt_cl' id='amt_cl' maxlength='4' size='5' value=".number_format( $row['Amount_r'],'0','.',',')."></td>";
		}else if ($type=='vcl' ){
			//$html .="<td  align='right'>". number_format($row['RemainHOAmt'],'0','.',',')."</span>&nbsp;&nbsp;/&nbsp;&nbsp;". number_format( $row['Amount_r'],'0','.',',')."</td>";
			$html .="<td  align='right'>". number_format( $row['Amount_r'],'0','.',',')."</td>";
			$html .="<td  align='right'>". number_format( $row['Amount_c'],'0','.',',')."</td>";
		}
		 ///else{
			// $html .="<td  align='center'><input type='text'  style='text-align:right;' class='amt_rc' name='amt_rc' size='5' value=".$row['Amount']."></input></td>";
		// }
		return $html;	
	}
?>